package domain;

import java.util.*;
import java.sql.*;

public class Product implements java.io.Serializable {
	String id;
	String name;
	double price;
	String category;
	int pnum;
	String imgurl;
	String description;
	int select;
	
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public double getPrice() {
		return price;
	}
	public void setPrice(double price) {
		this.price = price;
	}
	public String getCategory() {
		return category;
	}
	public void setCategory(String category) {
		this.category = category;
	}
	public int getPnum() {
		return pnum;
	}
	public void setPnum(int pnum) {
		this.pnum = pnum;
	}
	public String getImgurl() {
		return imgurl;
	}
	public void setImgurl(String imgurl) {
		this.imgurl = imgurl;
	}
	public String getDescription() {
		return description;
	}
	public void setDescription(String description) {
		this.description = description;
	}
	public int getSelect () {
		return select;
	}
	public void setSelct (int select) {
		this.select = select;
	}
	public List<Product> searchAll () throws SQLException, ClassNotFoundException {
		Class.forName("com.mysql.jdbc.Driver");
		List<Product> res = new ArrayList<Product>();
		Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/bookstore", "root", "");
		String strSQL = "select * from products;";
		PreparedStatement pst = conn.prepareStatement(strSQL);
		ResultSet rs = pst.executeQuery();
		
		while (rs.next()) {
			Product p = new Product();
			p.setId(rs.getString(1));
			p.setName(rs.getString(2));
			p.setPrice(rs.getDouble(3));
			p.setCategory(rs.getString(4));
			p.setPnum(rs.getInt(5));
			p.setImgurl(rs.getString(6));
			p.setDescription(rs.getString(7));
			res.add(p);
		}
		
		rs.close();
		pst.close();
		conn.close();
		
		return res;
	}
	public List<Product> search (String search) throws SQLException, ClassNotFoundException {
		Class.forName("com.mysql.jdbc.Driver");
		List<Product> res = new ArrayList<Product>();
		Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/bookstore", "root", "");
		String strSQL = "select * from products where name LIKE '" + search + "';";
		PreparedStatement pst = conn.prepareStatement(strSQL);
		ResultSet rs = pst.executeQuery();
		
		while (rs.next()) {
			Product p = new Product();
			p.setId(rs.getString(1));
			p.setName(rs.getString(2));
			p.setPrice(rs.getDouble(3));
			p.setCategory(rs.getString(4));
			p.setPnum(rs.getInt(5));
			p.setImgurl(rs.getString(6));
			p.setDescription(rs.getString(7));
			res.add(p);
		}
		
		rs.close();
		pst.close();
		conn.close();
		
		return res;
	}
	public boolean add () throws SQLException, ClassNotFoundException {
		Class.forName("com.mysql.jdbc.Driver");
		Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/bookstore", "root", "");
		Statement stmt = conn.createStatement();
		String insertSql = "insert into products(id,name,price,category,pnum,imgurl,description) values";
		
		insertSql += "(";
		insertSql += "\"" + UUID.randomUUID().toString() + "\",";
		insertSql += "\"" + this.name + "\",";
		insertSql += "\"" + this.price + "\",";
		insertSql += "\"" + this.category + "\",";
		insertSql += "\"" + this.pnum + "\",";
		insertSql += "\"" + this.imgurl + "\",";
		insertSql += "\"" + this.description + "\"";
		insertSql += ")";
		
		stmt.executeUpdate(insertSql);
		stmt.close();
		conn.close();
		
		return true;
	}
}
